//
//  FirstTravelDiaryCollectionViewCell.m
//  BTRIP
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "FirstTravelDiaryCollectionViewCell.h"
#import "TravelDiaryTableViewCell.h"
#import <AFHTTPSessionManager.h>
#import "TravelDiaryModel.h"
#import "FirstDetailTravelViewController.h"
#import <MJRefresh.h>

@interface FirstTravelDiaryCollectionViewCell ()
@property (assign, nonatomic)BOOL firstLoad;
@end
@implementation FirstTravelDiaryCollectionViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc{
    [_tableView release];
    [_travelAllArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.travelAllArray = [NSMutableArray array];
        [self creatTableView];
//        [self loadData];
        //下拉刷新
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        [self.tableView.header beginRefreshing];
        self.firstLoad = NO;
        //上拉加载
        self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return self;
}
#pragma mark 上拉加载
- (void)loadMoreData{
   [self.tableView.footer beginRefreshing];
    self.page++;
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:[NSString stringWithFormat:@"http://chanyouji.com/api/trips/featured.json?page=%ld",self.page] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
         NSArray *array = responseObject;
        for (NSDictionary *dic in array) {
            TravelDiaryModel *travel = [[TravelDiaryModel alloc]init];
            [travel setValuesForKeysWithDictionary:dic];
            [self.travelAllArray addObject:travel];
            [travel release];
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
        }

    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"请求失败:error: %@",error);
    }];
}



#pragma mark 创建tableView
- (void)creatTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 101) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.contentView addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView release];
    //注册标识
    [self.tableView registerClass:[TravelDiaryTableViewCell class] forCellReuseIdentifier:@"travelDiaryCell"];
}
#pragma mark 解析游记数据
- (void)loadData{
    if (_firstLoad == NO) {
        //如果不是第一次加载,把数组里的所有对象移除
        [self.travelAllArray removeAllObjects];
    }
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:@"http://chanyouji.com/api/trips/featured.json?page=1" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSArray *array = responseObject;
        for (NSDictionary *dic in array) {
            TravelDiaryModel *travel = [[TravelDiaryModel alloc]init];
            [travel setValuesForKeysWithDictionary:dic];
            [self.travelAllArray addObject:travel];
            [travel release];
            self.page = 1;                          
        }
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"请求失败 %@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.travelAllArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.travelAllArray.count != 0) {
        TravelDiaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"travelDiaryCell"];
        cell.travel = [self.travelAllArray objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:0.937 green:0.937 blue:0.957 alpha:1];
//         cell.backgroundColor = [UIColor blackColor];
        return cell;
    }else{
        NSLog(@"错误信息");
        return nil;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KHEIGHT / 2.5;
}
#pragma mark  cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   NSString*firstDetail = [NSString stringWithFormat:@"%@",[[self.travelAllArray objectAtIndex:indexPath.row] id]];
 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
#pragma mark 代理传值
    [self.delegate nextPage:firstDetail];
#pragma mark 消息中心
    //发送消息
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"detail" object:firstDetail];
    
    
    
}



@end
